package com.assistant.action;

import java.util.Date;

import org.utils.ActionUtils;
import org.utils.StringUtils;

import com.assistant.model.User;
import com.assistant.service.UserService;


public class UserAction extends ActionUtils {

	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	private User user;
	public void register() {
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		if(StringUtils.isNull(phone)){
			json.put("result", "fail");
			json.put("message", "手机号不能为空");
		}else if(StringUtils.isNull(name)){
			json.put("result", "fail");
			json.put("message", "姓名不能为空");
		}else if(StringUtils.isNull(password)){
			json.put("result", "fail");
			json.put("message", "密码不能为空");
		}else{
			json.put("result", "success");
			json.put("message", "注册成功");
			User user = new User();
			user.setName(name);
			user.setPhone(phone);
			user.setPassword(password);
			user.setDate(new Date());
			userService.saveOrUpdate(user);
		}
		render(json.toString());
	}

	public void login(){
		String password = request.getParameter("password");
		String phone = request.getParameter("phone");
		if(StringUtils.isNull(phone)){
			json.put("result", "fail");
			json.put("message", "手机号不能为空");
		}else if(StringUtils.isNull(password)){
			json.put("result", "fail");
			json.put("message", "密码不能为空");
		}else{
			json.put("result", "fail");
			json.put("message", "注册成功");
			User user = new User();
			user.setPhone(phone);
			user = userService.queryUser(user);
			if(user!=null&&user.getPassword().equals(password)){
				json.put("result", "success");
				json.put("message", "登陆成功");
			}else{
				json.put("result", "fail");
				json.put("message", "用户名或密码错误");
			}
		}
		render(json.toString());
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
